package com.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.beans.PaperInformation;
import com.example.beans.WorkInfoemation;
import com.example.myapplication.PaperInfoActivity;
import com.example.myapplication.R;
import com.example.myapplication.SiginActivity;
import com.example.myapplication.TeacherInfoActivity;
import com.example.widgets.CircleImageView;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/7.
 */
public class WorkinfoRecylerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<WorkInfoemation> list = new ArrayList<>();
    private LayoutInflater mInflater;
    BitmapUtils bitmapUtils;
    private Context context;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private int totalSize;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_ITEM) {
            View view = mInflater.inflate(R.layout.list_workifo_reclyerview, parent,false);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            //view.setBackgroundColor(Color.RED);
            ViewHolder viewHolder = new ViewHolder(view,onClick);
            bitmapUtils = new BitmapUtils(context);
            return viewHolder;
        }else if(viewType == TYPE_FOOTER){
            View view = mInflater.inflate(R.layout.list_workinfo_footer,parent,false);
            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
            FootsHolder footsHolder = new FootsHolder(view);
            return footsHolder;
        }
        return null;
    }
    @Override
    public int getItemViewType(int position) {
        if(list.size()>0) {
            return list.get(position).getType();
        }
        return 0;
    }

    viewHolderClickListener onClick = new viewHolderClickListener() {
        @Override
        public void onContextListen(WorkInfoemation workInfo, int position) {
            Log.i("kskks",workInfo.getWorkid());
            Log.i("kskksks",position+"");
            String workid = workInfo.getWorkid();
            Intent intent = new Intent();
            intent.setClass(context,PaperInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("workid",workid);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }

        @Override
        public void onUserClickListen(WorkInfoemation workInfo, int position) {

            String teacher_id = workInfo.getTeacher_id();
            String teacher_number =workInfo.getTeacher_number();
            Intent intent = new Intent();
            intent.setClass(context, TeacherInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("teacher",workInfo);
            intent.putExtras(bundle);
            context.startActivity(intent);
        }
    };


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ViewHolder) {
            ((ViewHolder)holder).teacher_name.setText(list.get(position).getTeacher_name());

            ((ViewHolder)holder).content.setText(list.get(position).getWorkinfo());
            ((ViewHolder)holder).time.setText(list.get(position).getDate());
            ((ViewHolder)holder).title.setText(list.get(position).getWorkname());
            // 加载网络图片
            bitmapUtils.display( ((ViewHolder)holder).Iv_teacher, "http://192.168.191.1/app/pic" + list.get(position).getTeacher_pic());
            WorkInfoemation workInfo = list.get(position);
            ((ViewHolder) holder).bind(workInfo);

        }
        //  holder.Iv_teacher.setImageBitmap(list.get(position).getTeacher_pic());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public WorkinfoRecylerView(Context context,List<WorkInfoemation> list) {
        this.list = list;
        this.mInflater= LayoutInflater.from(context);
        this.context = context;

    }


    public void changeList(List<WorkInfoemation> list){

        this.list = list;
        notifyDataSetChanged();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView title, content, time, teacher_name;
        private CircularImageView Iv_teacher;
        private LinearLayout linear_info,linear_context;
        private viewHolderClickListener mlistener;
        private CardView cardView ;

        public ViewHolder(View view,viewHolderClickListener listener) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            content = (TextView) view.findViewById(R.id.content);
            mlistener = listener;
            time = (TextView) view.findViewById(R.id.time);
            cardView = (CardView) view.findViewById(R.id.cardview);
            teacher_name = (TextView) view.findViewById(R.id.teacher_name);
            Iv_teacher = (CircularImageView) view.findViewById(R.id.Iv_teacher);
            linear_context = (LinearLayout) view.findViewById(R.id.linear_context);
            linear_info = (LinearLayout) view.findViewById(R.id.linear_info);
            teacher_name.setOnClickListener(this);
            cardView.setOnClickListener(this);
            Iv_teacher.setOnClickListener(this);
        }

        public void bind(WorkInfoemation workInfo) {
            linear_info.setTag(workInfo);
            linear_context.setTag(workInfo);
            teacher_name.setTag(workInfo);
            Iv_teacher.setTag(workInfo);
            cardView.setTag(workInfo);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.cardview:
                    mlistener.onContextListen((WorkInfoemation) linear_info.getTag(),getLayoutPosition());
                    break;
                case R.id.teacher_name:
                    mlistener.onUserClickListen((WorkInfoemation) linear_context.getTag(),getLayoutPosition());
                    break;
                case R.id.Iv_teacher:
                    mlistener.onUserClickListen((WorkInfoemation) linear_context.getTag(),getLayoutPosition());
            }
        }
    }
    public interface viewHolderClickListener{
        void onContextListen(WorkInfoemation workInfo,int position);
        void onUserClickListen(WorkInfoemation workInfo,int position);
    }


    public static class FootsHolder extends  RecyclerView.ViewHolder {

        public FootsHolder(View itemView) {
            super(itemView);

        }
    }


}
